package view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import com.example.xuegengwang.xuegengwang.R;

import common.Constant;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;
import utils.MyLogUtils;

/**
 * Created by Administrator on 2017/5/18.
 * 自定义头部下拉刷新上拉加载控件
 */

public class MyPtrRefresh_LoadView extends PtrClassicFrameLayout {
    private Context context;
    private MyStoreHouseHeader myStoreHouseHeader;
    private ListView mListView;
    private MyPtrHandler myPtrHandler;
    private static final String TAG = "MyPtrRefresh_LoadView";
    private View footerView;
    private int lastCount;
    private MyOnScrollListener myOnScrollListener;
    private boolean refresh = true;//开启刷新
    private boolean load = true;//开启加载


    public MyPtrRefresh_LoadView(Context context) {
        super(context, null);
    }

    public MyPtrRefresh_LoadView(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
        this.context = context;
        initView();
    }

    public MyPtrRefresh_LoadView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    private void initView() {
        this.setResistance(0.7f);
        this.setRatioOfHeaderHeightToRefresh(1.2f);
        this.setDurationToCloseHeader(400);//头部返回速度
        // default is false
        this.setPullToRefresh(false);//下拉的过程中也能刷新
        // default is true
        this.setKeepHeaderWhenRefresh(true);//刷新时保证头部还在
        this.disableWhenHorizontalMove(true);//处理左右滑动冲突

        footerView = LayoutInflater.from(context).inflate(R.layout.listview_footer, null);//自定义脚布局

        //添加自定义刷新头部
        myStoreHouseHeader = new MyStoreHouseHeader(context);
        this.setHeaderView(myStoreHouseHeader);
        this.addPtrUIHandler(myStoreHouseHeader);

        this.setPtrHandler(new PtrDefaultHandler2() {

            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                if (myPtrHandler != null) {
                    myPtrHandler.onLoadMoreBegin(frame);
                }
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                if (myPtrHandler != null) {
                    myPtrHandler.onRefreshBegin(frame);
                    reSetUI();
                }
            }
        });
    }

    //改变模式，实现可以控制是否要上拉和下拉
    public void ChangeMode(final boolean refresh, final boolean load) {
        this.refresh = refresh;
        this.load = load;
        this.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                if (refresh) {
                    return super.checkCanDoRefresh(frame, content, header);
                } else {
                    return false;
                }
            }

            @Override
            public boolean checkCanDoLoadMore(PtrFrameLayout frame, View content, View footer) {
                if (load) {
                    return super.checkCanDoLoadMore(frame, content, footer);
                } else {
                    return false;
                }
            }

            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                if (myPtrHandler != null) {
                    myPtrHandler.onLoadMoreBegin(frame);
                }
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                if (myPtrHandler != null) {
                    myPtrHandler.onRefreshBegin(frame);
                    reSetUI();
                }
            }
        });
    }

    public void OnRefreshComplete() {
        this.refreshComplete();
        if (load) {
            if (mListView != null) {
                int thisCount = mListView.getAdapter().getCount();
                if (lastCount >= thisCount) {
                    setNoMoreLoad();
                } else if ((thisCount - lastCount) < Constant.DATA_COUNT) {
                    //一次加载不足10条数据
                    setNoMoreLoad();
                } else {
                    lastCount = mListView.getAdapter().getCount();
                }
            }
        }
    }

    //刷新重置脚布局
    private void reSetUI() {
        lastCount = 0;//重置数组个数
        if (refresh && load) {
            this.setMode(Mode.BOTH);
        } else if (refresh && !load) {
            this.setMode(Mode.REFRESH);
        } else if (!refresh && load) {
            this.setMode(Mode.LOAD_MORE);
        } else {
            this.setMode(Mode.NONE);
        }
        if (mListView != null) {
            if (mListView.getFooterViewsCount() > 0) {
                for (int i = 0; i < mListView.getFooterViewsCount(); i++) {
                    mListView.removeFooterView(footerView);
                }
            }
        } else {
            MyLogUtils.e(TAG, "mListView == null");
        }
    }

    //没有更多数据了，需要关闭上拉刷新并且添加没有更多数据的布局
    private void setNoMoreLoad() {
        if (mListView != null) {
            if (mListView.getFooterViewsCount() <= 0) {
                mListView.addFooterView(footerView,null,false);
                this.setMode(Mode.REFRESH);
            }
        } else {
            MyLogUtils.e(TAG, "mListView == null");
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right,
                            int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        // 初始化ListView对象
        if (mListView == null) {
            getListView();
        }
    }

    private void getListView() {
        int childs = this.getChildCount();
        if (childs > 0) {
            for (int i = 0; i < childs; i++) {
                View childView = getChildAt(i);
                if (childView instanceof ListView) {
                    mListView = (ListView) childView;
                    mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
                        @Override
                        public void onScrollStateChanged(AbsListView view, int scrollState) {

                        }

                        @Override
                        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                            //listview滑动监听传递出去
                            if (myOnScrollListener != null) {
                                myOnScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
                            }
                        }
                    });
                }
            }
        }
    }

    public void setMyPtrHandler(MyPtrHandler myPtrHandler) {
        this.myPtrHandler = myPtrHandler;
    }

    public void initListCount(){
        lastCount = 0;
    }

    public interface MyPtrHandler {

        void onLoadMoreBegin(PtrFrameLayout frame);

        void onRefreshBegin(PtrFrameLayout frame);
    }


    public void setMyScrollListener(MyOnScrollListener myOnScrollListener) {
        this.myOnScrollListener = myOnScrollListener;
    }

    public interface MyOnScrollListener {
        void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount);
    }

}
